package com.easygo.controller;

import com.easygo.client.ContentCategoryClient;
import com.easygo.client.ContentClient;
import com.easygo.common.PageUtils;
import com.easygo.common.ResultResponse;
import com.easygo.pojo.Content;
import com.easygo.pojo.ContentCategory;
import org.springframework.context.annotation.Scope;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Controller
@Scope("prototype")
public class ContentController {

    @Resource
    ContentClient contentClient;

    @Resource
    ContentCategoryClient contentCategoryClient;

    @Resource
    RedisTemplate redisTemplate;

    /**
     * 01-分页
     * @param pageIndex
     * @param pageSize
     * @return
     */
    @RequestMapping("/content_page/{pageIndex}/{pageSize}")
    public String content_page(@PathVariable("pageIndex") Long pageIndex, @PathVariable("pageSize") Long pageSize ,Model model){
        System.out.println("pageIndex="+pageIndex);
        System.out.println("pageSize="+pageSize);
        PageUtils<Content> pageUtils = contentClient.getContentPage(pageIndex, pageSize);
        model.addAttribute("pageUtils",pageUtils);
        return "content"; //跳转模板
    }

    @PostMapping("/content_add")
    @ResponseBody
    public ResultResponse add(Content content){
        System.out.println("调用端："+content);
        redisTemplate.delete("mydata");//清空缓存！
        return contentClient.content_add(content);
    }

    @ResponseBody
    @PostMapping("/contentCategory_list")
    public List<ContentCategory> getcontentCategorys(){
        return contentCategoryClient.getcontentCategorys();
    }


}
